home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / PaletteAnimation gray / PaletteAnimation.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  4.2 KB  |  158 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                       Palette Demo                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*              Developer Technical Support             */
  6. /*                 Apple Computer, Inc.                 */
  7. /*                       08/15/90                       */
  8. /*______________________________________________________*/
  9.  
  10. #include    <Quickdraw.h>
  11. #include    <Windows.h>
  12. #include    <Events.h>
  13. #include     <GestaltEqu.h>
  14. #include    <OSEvents.h>
  15. #include    <Palettes.h>
  16. #include    <SegLoad.h>
  17. #include    <TextUtils.h>
  18.  
  19. extern _DataInit();
  20.  
  21. #define    TRUE            0xFF
  22. #define    FALSE            0
  23.  
  24. #define    clutID            40
  25. #define    numcolor        256
  26.  
  27. #ifdef powerc
  28.    QDGlobals    qd;
  29. #endif
  30.  
  31. Rect                WinMinusScroll;
  32. WindowPtr            myWindow;
  33. CTabHandle            mycolors;
  34. PaletteHandle        srcPalette;
  35. Boolean                DoneFlag;
  36.  
  37. /*______________________________________________________*/
  38. /*             Set Up Usage of Palette                  */
  39. /*______________________________________________________*/
  40. void Draw()
  41. /* The drawing routine is a collection of 253 random Rects which are colored with 
  42. the corresponding color in the color table. */
  43. {
  44.     float                    left, top, right, bottom;
  45.     Rect                    CRect;
  46.     int                        i;
  47.  
  48.     EraseRect (&WinMinusScroll);
  49.     for (i = 1; i < numcolor-2; ++i) {
  50.         do {
  51.             left = Random() / 32767.0 * WinMinusScroll.right;
  52.             if (left < 0)
  53.                 left = -left;
  54.             } while (left > WinMinusScroll.right - 40);
  55.             
  56.         do {
  57.             right = Random() / 32767.0 * WinMinusScroll.right;
  58.             if (right < 0)
  59.                 right = -right;
  60.             } while (right < left);
  61.             
  62.         do {
  63.             top = Random() / 32767.0 * WinMinusScroll.bottom;
  64.             if (top < 0)
  65.                 top = -top;
  66.             } while (top > WinMinusScroll.bottom - 40);
  67.             
  68.         do {
  69.             bottom = Random() / 32767.0 * WinMinusScroll.bottom;
  70.             if (bottom < 0)
  71.                 bottom = -bottom;
  72.             } while (bottom < top);
  73.         SetRect(&CRect, left, top, right, bottom);
  74.         PmForeColor(i);
  75.         FrameRect (&CRect);
  76.         PaintRect (&CRect);
  77.         }
  78. }
  79.  
  80.  
  81. /*______________________________________________________*/
  82. /*               Initialization traps                   */
  83. /*______________________________________________________*/
  84. void init()
  85. {
  86.     Rect                BaseRect;
  87.     OSErr                err;
  88.     long                feature;
  89.     
  90.     UnloadSeg(_DataInit);
  91.     InitGraf(&qd.thePort);
  92.     FlushEvents(everyEvent, 0);
  93.     InitWindows();
  94.     InitCursor();
  95.     
  96.     DoneFlag = FALSE;
  97. /*______________________________________________________*/
  98. /*            Use Gestalt to find 32BQD                 */
  99. /*______________________________________________________*/
  100.     err = Gestalt(gestaltQuickdrawVersion, &feature);
  101.     if (!err) {
  102.         if ((feature & 0x0f00) != 0x0200)
  103.             DoneFlag = TRUE;
  104.         }
  105.     else 
  106.         DoneFlag = TRUE;
  107. /*______________________________________________________*/
  108. /*                     Set Rects                        */
  109. /*______________________________________________________*/
  110.     SetRect(&BaseRect, 40, 60, 472, 282);
  111.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  112.                 BaseRect.bottom - 80);
  113. /*______________________________________________________*/
  114. /*        Open Window & set Palette & Picture           */
  115. /*______________________________________________________*/
  116.     mycolors = GetCTable (clutID);
  117.  
  118.     myWindow = NewCWindow(nil, &BaseRect, "\pUsing Palette Manager", TRUE, documentProc, 
  119.                             (WindowPtr) -1, TRUE, 150);
  120.     SetPort((WindowPtr) myWindow);
  121.  
  122.     srcPalette = NewPalette (numcolor, mycolors, pmAnimated+pmExplicit, 0);
  123.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  124.     
  125.     Draw();
  126. }
  127.  
  128. main()
  129. {
  130.     EventRecord     myEvent;
  131.     int                yieldTime = 0;
  132.     RGBColor        changecolor;
  133. /*______________________________________________________*/
  134. /*                   Main Event loop                    */
  135. /*______________________________________________________*/
  136.     init();
  137.     for ( ;; ) {
  138.         if (DoneFlag)
  139.             ExitToShell();
  140.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  141.             switch (myEvent.what) {
  142.                 case mouseDown:
  143.                 case keyDown:
  144.                 case autoKey:
  145.                     DoneFlag = TRUE;
  146.                     break;
  147.                 default:
  148.                     break;
  149.                 }
  150.             }
  151.             
  152.         /* Animate the colors one step for eash event loop */
  153.         GetEntryColor (srcPalette, 1, &changecolor);
  154.         AnimatePalette (myWindow, mycolors, 2, 1, numcolor - 2);
  155.         AnimateEntry (myWindow, numcolor - 1, &changecolor);
  156.         Palette2CTab (srcPalette, mycolors);
  157.         }
  158. }